var express = require('express');
var router = express.Router();
var axios = require('axios');
var md5 = require('md5');
var base64 = require('base-64');
var utf8 = require('utf8');
var qs = require('qs');
var util = require('../config/index.js');



/* GET home page. */
router.get('/', function(req, res, next) {
    res.render('index', {
        title: 'Expressaaa',
        obj: {
            a: '123456',
            b: 'abcbdej',
            c: '55555'
        }
    });
});


router.post('/add', function(req, res, next) {
    console.log(req.body);
    const param = req.body.code; //短信验证码
    const to = req.body.phone; //需要发送的号码

    if (to == '') {

        res.render('fail', {
            message: '手机号不能为空',
            code: ''
        });

        return
    }

    if (param == '') {
        res.render('fail', {
            message: '发送内容不能为空',
            code: ''
        });

        return
    }

    util.getResult(param, to).then(function(response) {
        console.log(response.data);
        console.log(response.data.resp.respCode);
        if (response.data.resp.respCode == '000000') {
            res.render('success', {
                message: '发送成功',
                code: response.data.resp
            });
        } else {
            res.render('fail', {
                message: '请求失败',
                code: response.data.resp
            });
        }

    }, function(err) {
        console.log(err);
        res.status(500).send('database error').end();
    })
    /*
    const appId = 'a1aaada4f7cd4cb7b26b369abfeb1e29';
    const AccountSid = '47c67e5afd3b52770a78333d0b42eeb1';
    const token = 'bb37d1b0de23c8c6787ef46bf415f7a9';
    const templateId = 115957;

    const SoftVersion = '2014-06-30';
    const baseURL = 'https://api.ucpaas.com/';
    const timeStr = getTimeStr();
    const SigParameter = md5(AccountSid + token + timeStr).toUpperCase();
    // 使用MD5加密（账户Id + 账户授权令牌 + 时间戳），共32位。
    // token = '9073f8af7042446bb7c063b69f8238f2'

    const bytes = utf8.encode(AccountSid + ':' + timeStr);
    const Authorization = base64.encode(bytes);
    // Authorization 使用Base64编码（账户Id + 冒号 + 时间戳） 

    // 时间戳 new Date().getTime()
    // (AccountSid+token+时间戳)
    // 
    const url = `${baseURL}/${SoftVersion}/Accounts/${AccountSid}/Messages/templateSMS/?sig=${SigParameter}`;




    // {SoftVersion}/Accounts/{AccountSid}/{function}/{operation}?sig={SigParameter}
    axios({
        method: 'post',
        url: url,
        data: {templateSMS: {
                appId: appId,
                param: param,
                templateId: templateId,
                to: to
            }},
            headers:{
                "Content-Type":"application/json;charset=utf-8",
                "Accept":"application/json",
                "Authorization":Authorization
            }
    }).then(function(response) {
        console.log(response.data);
        console.log(response.data.resp.respCode);
        if(response.data.resp.respCode == '000000'){
            res.render('success',{
                message:'发送成功'
            })
        }else{
            res.render('success',{
                message:response.data.resp
            });
        }

    },function(err){
        console.log(err)
    });*/

});



module.exports = router;